package com.tem.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tem.system.service.ISystemLevelTypeService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import com.tem.common.tool.result.ResultUtil;
import org.springframework.beans.BeanUtils;
import com.tem.system.entity.SystemLevel;
import com.api.manage.pojo.system.SystemLevelBean;
import com.tem.system.service.ISystemLevelService;
import com.tem.common.core.base.BaseController;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 系统等级 前端控制器
 * </p>
 * @author 腾木
 * @since 2023-11-14
 */
@RestController
@RequestMapping("/system/systemLevel")
public class SystemLevelController extends BaseController{
    @Autowired
    private ISystemLevelService systemLevelService;
    @Autowired
    private ISystemLevelTypeService systemLevelTypeService;
    @ApiOperation(value = "系统等级列表")
    @RequestMapping("list")
    public ResultUtil<IPage<SystemLevelBean>> list(SystemLevelBean request){
        IPage<SystemLevelBean> listBody =  list_init(request);
        return ResultUtil.success(listBody);
    }

    @ApiOperation(value = "系统构建 -- 系统等级列表")
    @RequestMapping("initlist")
    public ResultUtil<IPage<SystemLevelBean>> initlist(SystemLevelBean request){
        IPage<SystemLevelBean> listBody =  list_init(request);
        List<SystemLevelBean> responseList = listBody.getRecords();
        if(!responseList.isEmpty()) {
            responseList.forEach(it -> {
                it.setSystemLevelTypes(systemLevelTypeService.getListBeanVsPermission(it.getSystemLevelId()));
            });
        }
        return ResultUtil.success(listBody);
    }
    private IPage<SystemLevelBean> list_init(SystemLevelBean request){
        QueryWrapper<SystemLevel> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("system_level_id");
        Page page = new Page<>(request.getCurrent(),request.getSize());
        IPage listBody = systemLevelService.page(page,wrapper);
        List<SystemLevel> entityList=listBody.getRecords();
        if(!entityList.isEmpty()) {
            List<SystemLevelBean> responseList = new ArrayList<>();
            entityList.forEach(it -> {
                SystemLevelBean bean = new SystemLevelBean();
                BeanUtils.copyProperties(it,bean);
                responseList.add(bean);
            });
            listBody.setRecords(responseList);
        }
        return listBody;
    }

    @PostMapping("save")
    @ApiOperation(value = "新增系统等级")
    public ResultUtil save(@RequestBody SystemLevel entity){
        //entity.setCreateTime(LocalDateTime.now());entity.setDeleted(0);
        if(entity.getSystemLevelId()!=null){
            systemLevelService.updateById(entity);
        }else{
            entity.setDeleted(0);
            systemLevelService.save(entity);
        }
        return ResultUtil.success();
    }

    @GetMapping("getById/{id}")
    @ApiOperation(value = "id查询系统等级")
    public ResultUtil<SystemLevelBean> getById(@PathVariable String id){
        SystemLevel entity = systemLevelService.getById(id);
        if(entity!=null){
            SystemLevelBean bean = new SystemLevelBean();
            BeanUtils.copyProperties(entity,bean);
            return ResultUtil.success(bean);
        }
        return ResultUtil.error("没有找到对象");
    }

    @PutMapping("update")
    @ApiOperation(value = "修改系统等级")
    public ResultUtil update(@RequestBody SystemLevel entity){
        systemLevelService.updateById(entity);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除系统等级")
    @DeleteMapping("delete/{id}")
    public ResultUtil delete(@PathVariable Long id){
        systemLevelService.removeById(id);
        return ResultUtil.success();
    }
}